home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / ibm / sym_r3.arc / SIM_MEM.C < prev    next >
C/C++ Source or Header  |  1991-11-27  |  8KB  |  367 lines

  1. #include <stdio.h>
  2. #include "sim.h"
  3.  
  4. #ifdef MSC
  5. #include <graph.h>
  6. #endif
  7.  
  8. unsigned char mem[ 4095];
  9.  
  10. /* arrays for symbols */
  11. extern unsigned int  sym_nmbr[ 4095];
  12. extern char sym_tbl[ 1023];
  13.  
  14. int sim_read( addr)
  15. int addr;
  16. {
  17.    int data, done;
  18.    char c;
  19.    char buff[80];
  20.  
  21.     done = data = 0;
  22.     /* Don't prompt user for values if we are disassembling */
  23.     if (!disassemble)
  24.     {
  25.     if (addr >= 0 && addr < 4)
  26.     {
  27.         if (mem[addr+4] != 0xFF)   /* FF in DDR = all outputs */
  28.         {
  29.         c = (char) ((int)'A' + addr);
  30.         sprintf(buff, "Enter read value for port %c: ", c);
  31.         showprompt( buff);
  32.         fgets( buff, 80, stdin);
  33.  
  34.         /* To prevent scrolling unecessarily  through memory */
  35.  
  36.         if ( 0 == sscanf( buff, "%d", &data))
  37.         {
  38.           sim_halt();
  39.           goto readend;
  40.         }
  41.         }
  42.         data = data & (~mem[addr+4]) | mem[addr] & mem[addr+4];
  43.         clearline( PROMPTLINE);
  44.         done = 1;
  45.     }
  46.     else if (addr == AD_REG)
  47.     {
  48.         showprompt( "Enter read value for ADC register: ");
  49.         fgets( buff, 80, stdin);
  50.         sscanf( buff, "%d", &data);
  51.         clearline( PROMPTLINE);
  52.         done = 1;
  53.     }
  54.     }
  55.     if (done) goto readend;
  56.     if (addr >= 4 && addr < 8)
  57.     data = 0xFF;    /* port data direction regs are write only */
  58.     else if ((addr == TIMER_CONTROL) && (mem[MASK_OPT_REG] & 0x40))
  59.     data = (mem[addr] & 0xF7) | 0x37;
  60.     else if (addr >= 0 && addr <= 4095)
  61.     data = mem[addr];
  62.     else if (addr == RW_A)
  63.     data = pgm_model.a;
  64.     else if (addr == RW_X)
  65.     data = pgm_model.x;
  66.     else if (addr == RW_SP)
  67.     data = pgm_model.sp;
  68. readend:
  69.     return(data);
  70. }
  71.  
  72. int sim_write( addr, data)
  73. int addr, data;
  74. {
  75.     if (!disassemble)
  76.     {
  77.     if (addr >= 0 && addr <= 0x7F)  /* permit write to RAM, ports only */
  78.         mem[addr] = data;
  79.     else if (addr == RW_A)
  80.         pgm_model.a = data;
  81.     else if (addr == RW_X)
  82.         pgm_model.x = data;
  83.     else if (addr == RW_SP)
  84.         pgm_model.sp = data;
  85.     if (addr == TIMER_CONTROL)
  86.         sim_timer_ctrl();
  87.     }
  88. }
  89.  
  90. int sim_writef( addr, data)
  91. int addr, data;
  92. /* write forced - Just like sim_write but can write to PROM */
  93. /* Called from non-simulation things like 'M' (mem mod) and 'L' (load sx) */
  94. {
  95.     if (addr <= 0x7F)
  96.     sim_write( addr, data);   /* handle special cases, all in RAM, I/O */
  97.     else
  98.     {
  99.         mem[addr] = data;     /* Always succeeds - can write to PROM */
  100.     }
  101. }
  102.  
  103. int sim_readf( addr)
  104. int addr;
  105. /* read forced - simply returns mem[addr] - no prompts, no nothing */
  106. /* Used by sim_timer to read TCR as if it were the timer - the user */
  107. /* program cannot read most bits of the TCR in MOR controlled mode */
  108. {
  109.     return( mem[addr] );
  110. }
  111.  
  112. int sim_bitread( addr, bit)
  113. int addr, bit;
  114. /* If bit is an output bit in a port, no need to ask user for value */
  115. /* in that case don't call sim_read, or it would ask */
  116. /* NOTE: returns byte value just like sim_read, not just one bit */
  117. /*    The bit argument is only used to check for out bit in port */
  118. /*    This is so the trace can show the value of the byte, which */
  119. /*    the processor really reads */
  120. {
  121.     int data;
  122.  
  123.     if (( addr >= 0 && addr < 4) && (mem[addr+4] & (1<<bit)) )
  124.     data = mem[addr];
  125.     else
  126.         data = sim_read( addr);
  127.     return( data);
  128. }
  129.  
  130. /*********************************************************************/
  131. int load_moto_hex( infile)
  132. FILE *infile;
  133. /* opens infile and reads S0, S1 and S9 records.  S0 records are ignored.
  134.    S9 records terminate input.  Checksums on all records are ignored.
  135. */
  136. {
  137.     int i, j, addr, data, state, count, error, cc, s9;
  138.  
  139.     state = 0;
  140.     s9 = 0;
  141.  
  142.     while ( (i = fgetc( infile)) != EOF && !s9)
  143.     {
  144.     j = (i >= 'A') ? i - 55 : i - '0'; /* if i is hex digit, j is value*/
  145.     switch( state)
  146.     {
  147.     case 0:       /* Look for 'S' = beginning of record */
  148.         if (i == 'S')
  149.         {
  150. #ifdef DEBUG
  151.     printf( "Found 'S'-");
  152. #endif
  153.         state = 1;
  154.         count = 0;
  155.         cc = 0;
  156.         addr = 0;
  157.         }
  158.         break;
  159.     case 1:       /* Get character after 'S' = record type */
  160.         if (i == '1')
  161.         state = 2;
  162.         else if (i == '9')
  163.         s9 = 1;       /* exit input loop */
  164.         else
  165.         state = 0;  /* Skip S0 records - look for next 'S' */
  166.         break;
  167.     case 2:      /* Get byte count for this record */
  168. #ifdef DEBUG
  169.     printf( "Count-");
  170. #endif
  171.         count <<= 4;
  172.         count += j;
  173.         cc++;
  174.         if (cc == 2)
  175.         {
  176. #ifdef DEBUG
  177.     printf( "  Count = %d\n", count);
  178. #endif
  179.         state = 3;
  180.         addr = 0;
  181.         cc = 0;
  182.         }
  183.         break;
  184.     case 3:     /* Get start address for this record */
  185. #ifdef DEBUG
  186.     printf( "Addr-");
  187. #endif
  188.         addr <<= 4;
  189.         addr += j;
  190.         cc++;
  191.         if (cc == 4)
  192.         {
  193.         state = 4;
  194.         data = 0;
  195.         cc = 0;
  196. #ifdef DEBUG
  197.     printf( "  Addr = %d\n", addr);
  198. #endif
  199.         count -= 2;
  200.         if (count == 1)
  201.             state = 0;     /* Skip checksum, look for 'S' */
  202.         }
  203.         break;
  204.     case 4:     /* Get data bytes, 2 chars each */
  205. #ifdef DEBUG
  206.     printf( "Data-");
  207. #endif
  208.         data <<= 4;
  209.         data += j;
  210.         cc++;
  211.         if (cc == 2)
  212.         {
  213.         sim_writef( addr, data);
  214. #ifdef DEBUG
  215.     printf( "Wrote %d to %d\n", data, addr);
  216. #endif
  217.         addr++;
  218.         data = 0;
  219.         cc = 0;
  220.         count--;
  221.         if (count == 1)
  222.             state = 0;  /* Skip checksum, look for 'S' */
  223.         }
  224.         break;
  225.     }  /* end switch */
  226.     } /* end while */
  227.     return( !s9);       /* Error is didn't end with S9 record */
  228. } /* end load_moto_hex */
  229.  
  230.  
  231.  
  232. int sym_writef_init()
  233. {
  234.  
  235.   int i;
  236.  
  237.   for( i=0; i<4096; i++)
  238.   {
  239.     sym_nmbr[ i] = i;
  240.   }
  241.  
  242. }
  243.  
  244. int sym_writef( sym_val, i)
  245. int sym_val,i;
  246. {
  247.     sym_nmbr[ i] = sym_val + 4096;
  248. }
  249.  
  250. int load_sym_data( infile)
  251. FILE *infile;
  252. {
  253.     int i, s9;
  254.  
  255.     char n[5];
  256.     static char *sym_tbl_ptr;
  257.  
  258.     s9 = 0;
  259.  
  260.     while( fgetc(infile) != '\f');
  261.  
  262.     sym_tbl_ptr = sym_tbl;
  263.  
  264.     while( fscanf( infile, "%s %5x", n, &i) != EOF)
  265.     {
  266.       sym_writef( sym_tbl_ptr - sym_tbl, i);
  267.       sym_tbl_ptr = sym_tbl_ptr + sprintf( sym_tbl_ptr, "%s", n) + 1;
  268.  
  269.     }
  270.  
  271.     return(!s9);
  272.  
  273. } /* end load_sym_data */
  274.  
  275. int load_sym( filename)
  276. char filename[];
  277. /* returns 0 if no error, 1 if error */
  278. {
  279.  
  280.     FILE *infile;
  281.     int retval;
  282.     char fname[64];
  283.     char *cP;
  284.     int i;
  285.     char n[5];
  286.  
  287.  
  288.     sym_writef_init();
  289.  
  290.     i = 0;
  291.     cP = fname;
  292.     while (filename[i] != '\n' && i < 64)
  293.     {
  294.     if (filename[i] != ' ' && filename[i] != '\x09')  /* skip white */
  295.         *cP++ = filename[i];
  296.     i++;
  297.     }
  298.     *cP++ = '\0';   /* Null terminate string */
  299.  
  300.     infile = fopen( fname, "r");
  301.     if (infile != NULL)
  302.     retval = load_sym_data( infile);
  303.     else
  304.     retval = 2;
  305.  
  306.     settextposition( PROMPTLINE,1);
  307.  
  308.     if (retval)
  309.     {
  310.        if (retval == 2)
  311.        printf  ("*** CANNOT OPEN FILE %s ***", fname);
  312.        else
  313.        printf( "*** ERROR LOADING FILE %s ***", fname);
  314.     }
  315.     else
  316.     {
  317.        printf( "File %s loaded", fname);
  318.     }
  319.     promptline_full = 1;
  320.     return( retval);
  321.  
  322. }/* end load_sym */
  323.  
  324. int load( filename)
  325. char filename[];
  326. /* returns 0 if no error, 1 if error */
  327. {
  328.     FILE *infile;
  329.     int retval;
  330.     char fname[64];
  331.     char *cP;
  332.     int i;
  333.  
  334.     i = 0;
  335.     cP = fname;
  336.     while (filename[i] != '\n' && i < 64)
  337.     {
  338.     if (filename[i] != ' ' && filename[i] != '\x09')  /* skip white */
  339.         *cP++ = filename[i];
  340.     i++;
  341.     }
  342.     *cP++ = '\0';   /* Null terminate string */
  343.  
  344.     infile = fopen( fname, "r");
  345.     if (infile != NULL)
  346.     retval = load_moto_hex( infile);
  347.     else
  348.     retval = 2;
  349.     settextposition( PROMPTLINE,1);
  350.     if (retval)
  351.     {
  352.        if (retval == 2)
  353.        printf  ("*** CANNOT OPEN FILE %s ***", fname);
  354.        else
  355.        printf( "*** ERROR LOADING FILE %s ***", fname);
  356.     }
  357.     else
  358.     {
  359.        printf( "File %s loaded", fname);
  360.        sim_reset();
  361.     }
  362.     promptline_full = 1;
  363.     return( retval);
  364. }/* end load */
  365.  
  366.  
  367.